<?php

namespace App\Trip\Geo;


use Illuminate\Support\Collection;

abstract class GeoManager
{

    protected RedisGeo $geo;

//    protected function

    public function nearestCode(string $longitude, string $latitude, int $distance, string $unit): string
    {
        return $this->nearCodes($longitude,
            $latitude,
            $distance,
            $unit,
            1, 'ASC')?->first() ?: '';
    }

    /**
     * @param string $longitude 经度
     * @param string $latitude 纬度
     * @param int    $distance
     * @param string $unit m, km
     * @param int    $count 1-N
     * @param string $sort ASC ,DESC
     * @return Collection
     */
    public function nearCodes(string $longitude, string $latitude, int $distance, string $unit, int $count, string $sort): Collection
    {
        return collect($this->geo->radius($longitude, $latitude, $distance, $unit, $count, $sort))->keys();
    }

    public function addGeoByLocation(Location $location, string $code): void
    {
        $this->addGeo($location->getLongitude(), $location->getLongitude(), $code);
    }

    public function addGeo(string $longitude, string $latitude, string $code): void
    {
        $longitude && $latitude && $this->geo->add($longitude, $latitude, $code);
    }

    public function delGeo(string $code): void
    {
        $this->geo->delete($code);
    }

    public function hasGeo(string $code): bool
    {
        return (bool)$this->geo->hash($code);
    }
}